package cn.bookCity.daoimpl;

import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.sun.org.apache.bcel.internal.generic.NEW;

import cn.bookCity.dao.UserDao;
import cn.bookCity.domain.User;
import cn.bookCity.util.DataSourceUtil;


public class UserDaoImpl implements UserDao {
	
	

	@Override
	public void regist(User user) {
		QueryRunner qr=new QueryRunner(DataSourceUtil.getDataSource());
		try {
			String sql="insert into user values(?,?,?,?,?,?,?,?,?,?,?);";
			qr.update(sql,user.getId(),user.getUsername(),user.getPassword(),user.getGender(),user.getEmail(),user.getTelephone(),user.getIntroduce(),
					user.getActiveCode(),user.getState(),user.getRole(),user.getRegistTime());
//			qr.update(sql,user.getId(),user.getUsername(),user.getPassword(),user.getActiveCode());
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

	@Override
	public User selectUserByCode(String code) {
		QueryRunner qr=new QueryRunner(DataSourceUtil.getDataSource());
		try {
			String sql="select * from user where activeCode=? limit 1";
			return qr.query(sql, new BeanHandler<>(User.class),code);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void UpdateUserByUser(User user) {
		QueryRunner qr=new QueryRunner(DataSourceUtil.getDataSource());
		try {
			String sql="update user set username=?,password=?,gender=?,email=?,telephone=?,introduce=?,activeCode=?,state=?,role=? where id=? ";
			qr.update(sql,user.getUsername(),user.getPassword(),user.getGender(),user.getEmail(),user.getTelephone(),user.getIntroduce(),
					user.getActiveCode(),user.getState(),user.getRole(),user.getId());
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
		
	}

	@Override
	public User selectUserByNP(String username, String password) {
		QueryRunner qr=new QueryRunner(DataSourceUtil.getDataSource());
		try {
			String sql="select * from user where username=? AND password=?";
			return qr.query(sql, new BeanHandler<>(User.class),username,password);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public User selectUserByEamil(String email) {
		// TODO Auto-generated method stub
		 QueryRunner qr=new QueryRunner(DataSourceUtil.getDataSource());	
		try {
			String sql="select * from user where email=?";
			
			return qr.query(sql, new BeanHandler<>(User.class),email);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public User selectUserName(String name) {
		// TODO Auto-generated method stub
				 QueryRunner qr=new QueryRunner(DataSourceUtil.getDataSource());	
				try {
					String sql="select * from user where username=?";
					
					return qr.query(sql, new BeanHandler<>(User.class),name);
				} catch (SQLException e) {
					throw new RuntimeException(e);
				}
	}

	@Override
	public List<User> get() {
		 QueryRunner qr=new QueryRunner(DataSourceUtil.getDataSource());	
			try {
				String sql="select * from user limit 1,50";
				
				return qr.query(sql, new BeanListHandler<>(User.class));
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
	}

	@Override
	public void del(String uid) {
		// 家删除
		QueryRunner qr=new QueryRunner(DataSourceUtil.getDataSource());
		try {
			String sql="update user set state=2 where id=? ";
			qr.update(sql,uid);
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
